package com.jyhz.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.jyhz.admin.utils.HttpClient4Utils;
import com.jyhz.admin.utils.SignatureUtils;
import com.jyhz.admin.verifyCode.VerificationCode;
import com.jyhz.common.dto.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.apache.http.client.HttpClient;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author: 谢君臣
 * @create: 2020-08-22 17:41
 * @founction: 发送短信
 */
@Service
@Slf4j
public class SendMsgServiceImpl {

    public Boolean sendMsgService(VerificationCode vcode) {
        log.info("===>>>短信发送异步方法体");
        Boolean flag = false;
        String mobile = vcode.getUsername();
        String code = vcode.getCode();
        String templateId = "11562";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        HttpResult httpResult = new HttpResult();
        try {
            String result = send(mobile, jsonObject, templateId);
//            String  result ="{\"code\":200,\"msg\":\"ok\",\"data\":{\"result\":200,\"requestId\":\"a687878e5c0c4d89acb57e4a33a77369\"}}";
            httpResult = JSONObject.parseObject(result, HttpResult.class);
            if (httpResult.code==200)
            {
                flag = true;
                log.info("===>>>短信发送成功");
            }
            else
                throw new Exception();
        } catch (Exception e) {
            log.info("===>>>短信发送失败，通知运维人员");
            /**
             *  做持久化处理
             */
        }
        return flag;
    }
    /**
     * 业务ID，易盾根据产品业务特点分配
     */
    private final static String BUSINESSID = "7d39a1acb6ce46419e347af509ace805";
    /**
     * 产品密钥ID，产品标识
     */
    private final static String SECRETID = "7394ab3ea82b71764171ffb178219405";
    /**
     * 产品私有密钥，服务端生成签名信息使用，请严格保管，避免泄露
     */
    private final static String SECRETKEY = "2ca906c5aea1fa00eb9b3dfb6f2fb117";

    /**
     * 接口地址
     */
    private final static String API_URL = "https://sms.dun.163.com/v2/sendsms";
    /**
     * 实例化HttpClient，发送http请求使用，可根据需要自行调参
     */
    private static HttpClient httpClient = HttpClient4Utils.createHttpClient(100, 20, 10000, 2000, 2000);

    /**
     * @param
     * @throws Exception
     */
    private String send(String mobile,JSONObject jsonObject,String templateId) throws Exception {
        //        // 此处用申请通过的模板id
        //        String templateId = "xxx";
        //        // 模板参数对应的json格式数据,例如模板为您的验证码为${p1},请于${p2}时间登陆到我们的服务器
        //        JSONObject jsonObject = new JSONObject();
        //        jsonObject.put("p1", "123");
        //        jsonObject.put("p2", "20180816");
        //        String params = jsonObject.toJSONString();
        //        String mobile = "xx";
        String params = jsonObject.toJSONString();
        Map<String, String> datas = buildParam(mobile, templateId, params);
        String result = HttpClient4Utils.sendPost(httpClient, API_URL, datas, Consts.UTF_8);
        return result;
    }

    private static Map<String, String> buildParam(String mobile, String templateId, String params) throws IOException {
        Map map = new HashMap<String, String>();
        map.put("businessId", BUSINESSID);
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        map.put("version", "v2");
        map.put("templateId", templateId);
        map.put("mobile", mobile);
        // 国际短信对应的国际编码(非国际短信接入请注释掉该行代码)
        // map.put("internationalCode", "对应的国家编码");
        map.put("paramType", "json");
        map.put("params", params);
        map.put("nonce", UUID.randomUUID().toString().replace("-", ""));
        map.put("secretId", SECRETID);
        String sign = SignatureUtils.genSignature(SECRETKEY, map);
        map.put("signature", sign);
        return map;
    }

}
